import React, { useEffect, useState } from 'react';
import { Spin } from 'antd';
import { connect } from 'umi';

import SalaryOverviewWrapper from '@/pages/SalaryManage/components/SalaryOverviewWrapper';
import salaryStyle from '@/pages/SalaryManage/salaryManage.less';

import { wrapSalaryPayoff } from '@/pages/SalaryManage/utils/pageWrapUtil';

import { fetchOrganizationListAndDefault } from '@/pages/SalaryManage/utils';

import modelType from '@/pages/SalaryManage/SalaryCalculate/constant/modelTypeConstant';

const namespace = 'overviewRecheckModel';
const shareNamespace = 'payoffSharedModel';
const DISPATCH_TYPE = {
    initType: `${namespace}/${modelType.E_INIT}`,
    [modelType.E_RESET]: `${namespace}/${modelType.E_RESET}`,
    searchCondition: `${namespace}/${modelType.E_SEARCH_CONDITION}`,
    searchType: `${namespace}/${modelType.E_SEARCH}`,
    sharedState: `${shareNamespace}/${modelType.E_SHARED_STATE}`
};

const OverViewPage = wrapSalaryPayoff(props => {
    const {
        sharePeriod,
        shareOrganizationCode,
        period,
        dispatch,
        tableLoading,
        dispatchType = DISPATCH_TYPE,
        checkButtonList = [],
        searchModel = {},
        dataSource,
        footerSource,
        rowKey
    } = props;
    const [organizationTreeData, setOrganizationTreeData] = useState({});

    useEffect(() => {
        const resetInit = async () => {
            dispatch({
                type: dispatchType.reset,
                payload: {
                    organizationCode: shareOrganizationCode
                }
            });

            const {
                organizationResponse: response,
                defaultOrganizationCode: defaultSelectCode
            } = await fetchOrganizationListAndDefault(shareOrganizationCode);
            setOrganizationTreeData(response);
            if (shareOrganizationCode !== defaultSelectCode) {
                dispatch({
                    type: dispatchType.sharedState,
                    payload: {
                        shareOrganizationCode: defaultSelectCode,
                        sharePeriod: undefined
                    }
                })
                dispatch({
                    type: dispatchType.reset,
                    payload: {
                        organizationCode: defaultSelectCode
                    }
                });
            }
        };
        resetInit().then(() => {
            dispatch({ type: dispatchType.initType, payload: sharePeriod });
        });
    }, []);
    return (
        <div className={salaryStyle.salaryWrapper}>
            <Spin spinning={tableLoading}>
                <SalaryOverviewWrapper
                    period={period}
                    dispatch={dispatch}
                    dispatchType={dispatchType}
                    checkButtonList={checkButtonList}
                    dataSource={dataSource}
                    footerSource={footerSource}
                    tableLoading={tableLoading}
                    searchModel={searchModel}
                    rowKey={rowKey}
                    organizationList={organizationTreeData}
            />
            </Spin>
        </div>
    );
});
export default connect(({ overviewRecheckModel, payoffSharedModel }) => ({
    ...overviewRecheckModel,
    ...payoffSharedModel
}))(OverViewPage);
